app.controller("administratorCtrl", ["$scope","$q","AdministratorApi","sweetAlertUtil","ngTableParams",
    function($scope,$q,AdministratorApi,sweetAlertUtil,ngTableParams) {

        $scope.memQueryParam={
            page:1,
            keyword:""
        }

        /*---------------------------start 获取管理员列表 -----------------------------*/
        paginations()
        function AdministratorList() {
            var defer = $q.defer();
            var params = {
                page: $scope.memQueryParam.page,
                keyword:$scope.memQueryParam.keyword
            };
            AdministratorApi.getAdministratorList(params).then(function(data){
                if (data.code != 200) {
                    sweetAlertUtil.fail(data.errMsg)
                    defer.reject(data.errMsg);
                    return;
                }
                $scope.resule = data.result.data;
                defer.resolve(data.result);
                $scope.isInit = true;
            })
            return defer.promise;
        }
        /*---------------------------end 获取管理员列表 -----------------------------*/

        /*---------------------------start 分页开始 -----------------------------*/
        function paginations() {
            $scope.tableParams = new ngTableParams({
                page: 1, // show first page
                count: 10 // count per page
            }, {
                //total: 0, // length of data
                counts: [],
                getData: function ($defer, params) {
                    // $scope.p_num = (params.page()-1)*10
                    $scope.memQueryParam['page'] = params.page();
                    AdministratorList().then(function (res) {
                        $scope.tableParams.total(res.total);
                        $defer.resolve(res.data);
                    });
                }
            });
        }

        /*---------------------------end 分页结束 -----------------------------*/
        
        /*---------------------------start 搜索 ------------------------------*/
        $scope.searchDate = function(){
            $scope.tableParams.page(1);
            $scope.tableParams.reload();
        }
        /*---------------------------end 搜索 ------------------------------*/

        /*---------------------------start 删除 ------------------------------*/
        $scope.del = function(item){
            if(item.role_tag == "ROOT"){
                sweetAlertUtil.success("超级管理员无法被删除")
                return
            }
            sweetAlertUtil.confirm("确认删除管理员?", function(isconfirm) {
                if (isconfirm) {
                    AdministratorApi.getAdministratorDel(item.id).then(function(res){
                        if(res.code == 200){
                            sweetAlertUtil.success("删除成功")
                            $scope.tableParams.reload();
                        }else{
                            sweetAlertUtil.success(res.msg)
                        }
                    },function(){})
                }

            })

        }
        /*---------------------------end 删除 ------------------------------*/        

    }
]);